package com.quanyan.assets.service.impl;

import com.quanyan.api.APIRequest;
import com.quanyan.api.APIResponse;
import com.quanyan.api.enums.ApiResponseEnum;
import com.quanyan.assets.emun.AssetsErrorEnum;
import com.quanyan.assets.emun.StateType;
import com.quanyan.assets.entity.TbAccumulateRule;
import com.quanyan.assets.entity.TbAccumulateRuleExample;
import com.quanyan.assets.entity.TbUserShellAccumulateHistory;
import com.quanyan.assets.mapper.TbAccumulateRuleMapper;
import com.quanyan.assets.mapper.TbUserShellAccumulateHistoryMapper;
import com.quanyan.assets.service.AssetsRuleService;
import com.quanyan.common.annotation.ServiceExceptionHandler;
import com.quanyan.common.exception.BizException;
import com.quanyan.common.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * Created by wmb on 2016/5/27.
 */
@Service("assetsRuleService")
public class AssetsRuleServiceImpl implements AssetsRuleService {
    @Autowired
    private TbAccumulateRuleMapper tbAccumulateRuleMapper;
    @Override
    @Valid
    public APIResponse addAccumulateRule(APIRequest<TbAccumulateRule> apiTbAccumulateRule){

        TbAccumulateRule TbAccumulateRule=apiTbAccumulateRule.getData();
        //参数校验
        addCheck(TbAccumulateRule);
        TbAccumulateRule.setState(StateType.EFFECTIVE.id.byteValue());
        try {
            tbAccumulateRuleMapper.insertSelective(TbAccumulateRule);
        }catch (Exception e){
            throw new BizException(AssetsErrorEnum.ASSETS_RULE_RECORD_EXISTS);
        }
        return APIResponse.returnSuccess(ApiResponseEnum.SUCCESS);
    }

    @Override
    @Valid
    @Transactional(rollbackFor = Exception.class)
    public APIResponse updateAccumulateRule(APIRequest<TbAccumulateRule> apiTbAccumulateRule) {
        TbAccumulateRule tbAccumulateRule=apiTbAccumulateRule.getData();
        if(null==tbAccumulateRule.getId()){
            throw new BizException(AssetsErrorEnum.ID_IS_NULL);
        }
        TbAccumulateRule updateTbAccumulateRule=tbAccumulateRuleMapper.selectByPrimaryKey(tbAccumulateRule.getId());
        //判断是否有存在此条记录
        if(null==updateTbAccumulateRule){
            throw new BizException(ApiResponseEnum.RECORD_DO_NOT_EXISTS);
        }
        tbAccumulateRule.setUpdateTime(new Date());
        //如果自定义获取数目，必须有最大值
        if((tbAccumulateRule.getRuleType()==null?updateTbAccumulateRule.getRuleType():tbAccumulateRule.getRuleType())==4&&(null==tbAccumulateRule.getMaxUseable()||tbAccumulateRule.getMaxUseable()==-1)
                &&(null==updateTbAccumulateRule.getMaxUseable()||updateTbAccumulateRule.getMaxUseable()==-1)){
            throw new BizException(AssetsErrorEnum.MAX_ACCUMULATE_IS_NULL);
        }
        if((tbAccumulateRule.getRuleType()==null?updateTbAccumulateRule.getRuleType():tbAccumulateRule.getRuleType())!=0
                &&(tbAccumulateRule.getRuleType()==null?updateTbAccumulateRule.getRuleType():tbAccumulateRule.getRuleType())!=4
                &&null==tbAccumulateRule.getAccumulateRule()&&null==updateTbAccumulateRule.getAccumulateRule()){
            throw new BizException(AssetsErrorEnum.ACCUMULATE_RULE_IS_NULL);
        }
        try{
            tbAccumulateRuleMapper.updateByPrimaryKeySelective(tbAccumulateRule);
        }
        catch (Exception e){
            throw new BizException(AssetsErrorEnum.ASSETS_RULE_RECORD_EXISTS);
        }
        return APIResponse.returnSuccess(ApiResponseEnum.SUCCESS);


    }

    //新增校验
    private void addCheck(TbAccumulateRule TbAccumulateRule){
        if(null==TbAccumulateRule){
            throw new BizException(AssetsErrorEnum.ASSETS_RULE_IS_NULL);
        }
        if(null==TbAccumulateRule.getSystemCode()){
            throw new BizException(AssetsErrorEnum.SYSTEM_CODE_IS_NULL);
        }
        if(null==TbAccumulateRule.getBusinessCode()){
            throw new BizException(AssetsErrorEnum.BUSINESS_CODE_IS_NULL);
        }
        if(null==TbAccumulateRule.getAssetsType()){
            throw new BizException(AssetsErrorEnum.ASSETS_ID_NULL);
        }
        if(null==TbAccumulateRule.getRuleType()){
            throw new BizException(AssetsErrorEnum.RULE_TYPE_IS_NULL);
        }else if(TbAccumulateRule.getRuleType()==4&&null==TbAccumulateRule.getMaxUseable()){
            throw new BizException(AssetsErrorEnum.MAX_ACCUMULATE_IS_NULL);
        }
        if(TbAccumulateRule.getRuleType()!=4&&TbAccumulateRule.getRuleType()!=0&&null==TbAccumulateRule.getAccumulateRule()){
            throw new BizException(AssetsErrorEnum.ACCUMULATE_RULE_IS_NULL);
        }
        if(null==TbAccumulateRule.getOpType()){
            throw new BizException(AssetsErrorEnum.OP_TYPE_IS_NULL);
        }
        if(null==TbAccumulateRule.getMaxUseable()){
            TbAccumulateRule.setMaxUseable(new Long(-1));
        }
    }

    //判断sourceSystemId+sourceBusinessId+assetsType是否已经存在记录
    private void isExist(TbAccumulateRule TbAccumulateRule){
        TbAccumulateRuleExample example=new TbAccumulateRuleExample();
        TbAccumulateRuleExample.Criteria criteria=example.createCriteria();
        criteria.andBusinessCodeEqualTo(TbAccumulateRule.getBusinessCode()).
                andSystemCodeEqualTo(TbAccumulateRule.getSystemCode()).andAssetsTypeEqualTo(TbAccumulateRule.getAssetsType());
        if(null!=TbAccumulateRule.getId()){
            criteria.andIdNotEqualTo(TbAccumulateRule.getId());
        }
        List<TbAccumulateRule> TbAccumulateRuleList=tbAccumulateRuleMapper.selectByExample(example);
        if(!TbAccumulateRuleList.isEmpty()){
            throw new BizException(AssetsErrorEnum.ASSETS_RULE_RECORD_EXISTS);
        }
    }
}
